/*
 * 创建人：王曙光
 * 创建时间：2011-9-10
 * 功能描述：登录页
 */
$(function() {
	//居中
	window.nativeWindow.x = (air.Capabilities.screenResolutionX - Const.login._width) / 2;
	window.nativeWindow.y = (air.Capabilities.screenResolutionY - Const.login._height ) / 2;

	//启用拖拽
	Common.DragStart($(".login_header")[0]);
	
	//最小化和关闭
	$(".min").bind("click", function(event) {
		
		window.nativeWindow.minimize();
	});
	$(".close").bind("click", function(event) {
		
		air.NativeApplication.nativeApplication.icon.bitmaps = [];
		air.NativeApplication.nativeApplication.exit();
	});
	//初始化页面数据
	var configinfo = App.getConfig();
	if(configinfo != null) {
		if(configinfo.RembPwd == "1") {
			$("#cbox_rembpwd")[0].checked = true;
			$("#tbox_pwd").val(configinfo.Password);
			$("#tbox_logincode").val(configinfo.LoginCode);
		} else {
			$("#cbox_rembpwd")[0].checked = false;
		}
		if(configinfo.AutoLogin == "1") {
			$("#cbox_autologin")[0].checked = true;
			if(window.returnValue != "1")
				SetLogin();
		} else {
			$("#cbox_autologin")[0].checked = false;
		}
	}

	//键盘控制
	$("#tbox_logincode").bind("keydown", function(event) {
		if(event.keyCode != 13)
			return;
		$("#tbox_pwd").focus();
	});
	$("#tbox_pwd").bind("keydown", function(event) {
		if(event.keyCode != 13)
			return;
		if(Const._isbusy)
			return;
		$("#btnLogin").trigger("click");
	});
	$("#tbox_logincode").focus();

	//登录按钮
	$("#btnLogin").bind("click", function() {
		SetLogin();
	});
	//设置
	$(".login_set").bind("click", function() {
		//打开新窗口
		//设置窗口
		Common.OpenSetWin();
	});
});
function SetLogin() {
	//登录验证
	var configinfo = App.getConfig();
	var pwd = $("#tbox_pwd").val();
	var loginCode = $("#tbox_logincode").val();
	if(pwd == "" || loginCode == "") {
		Common.ShowTips("用户密码不能为空");
		return;
	}
	var rembPwd = "";
	if($("#cbox_rembpwd")[0].checked == true) {
		rembPwd = "1";
	} else {
		rembPwd = "0";
	}
	var autoLogin = "";
	if($("#cbox_autologin")[0].checked == true) {
		autoLogin = "1";
	} else {
		autoLogin = "0";
	}
	ShowLoading();
	$("#btnLogin").attr("z-index", 1001);
	App.login(loginCode, pwd, rembPwd, autoLogin, function() {

		//打开主界面
		var winx = 0;
		var winy = 0;
		if(configinfo != null) {
			if(configinfo.WinHeight != "")
				Const.main._cheight = Math.round(configinfo.WinHeight);
			if(configinfo.WinWidth != "")
				Const.main._width = Math.round(configinfo.WinWidth);
			if(configinfo.WinStartX != "")
				winx = Math.round(configinfo.WinStartX);
			if(configinfo.WinStartY != "")
				winy = Math.round(configinfo.WinStartY);
		}
		//打开新窗口
		//登录窗口
		var options = new air.NativeWindowInitOptions();
		options.minimizable = false;
		options.maximizable = false;
		options.resizable = false;
		options.transparent = true;
		options.systemChrome = air.NativeWindowSystemChrome.NONE;
		var login = air.File.applicationDirectory.resolvePath('CC.html');
		if(winx == 0)
			winx = (air.Capabilities.screenResolutionX - Const.main._width) / 2;
		if(winy == 0)
			winy = (air.Capabilities.screenResolutionY - Const.main._height ) / 2;
		var bounds = new air.Rectangle(winx, winy, Const.main._width, Const.main._height);

		var win = air.HTMLLoader.createRootWindow(true, options, true, bounds);
		win.load(new air.URLRequest(login.url));
		HideLoading();		
		window.nativeWindow.close();
	}, function(data) {
		HideLoading();
		Common.ShowTips(data);
	});
}

function ShowLoading() {
	$(".loading").show();
	Const._isbusy = true;
}

function HideLoading() {
	$(".loading").hide();
	Const._isbusy = false;
}